|
|
|
|
|
|
|
The following sections describe ways you can use advanced text features to display text that you want the user to see but not modify. These techniques give you the equivalent of label controls that display multiple colors and fonts. |
|
|
|
|
|
|
|
|
The first section explains how you can use Visual Basic methods to create labels using more than one color or font. Later sections show how to use API functions to perform more powerful font operations. |
|
|
|
|
|
|
|
|
Multiple Colors and Fonts |
|
|
|
|
|
|
|
|
Label controls have a single Font property, so they can display text using only one font at a time. Labels also have a single ForeColor property, so all of the text contained in a label must be displayed in the same color. |
|
|
|
|
|
|
|
|
When you draw text directly on a form or picture box using the Print method, however, Visual Basic uses whatever values the form or picture box's Font object has at the time. If you draw some text, modify the Font object, and then draw more text, the two pieces of text are displayed in different styles. Similarly, you can change a form or picture box's ForeColor property to produce text of different colors. |
|
|
|
|
|
|
|
|
There is one drawback to this approach: You must draw the text yourself instead of using label controls. That means you must keep the screen uptodate whenever it is obscured by another window and then exposed. |
|
|
|
|
|
|
|
|
One approach is to redraw the text whenever your program receives a Paint event. This may make your program more complicated and it may be quite slow if the text you are redrawing is very complicated. |
|
|
|
|
|
|
|
|
Another approach to keeping the display uptodate is to set the form or picture box's AutoRedraw property to true. This is easier and quicker than responding to Paint events, but it makes your program use extra memory at run time. |
|
|
|
|
|
|
|
|
The RandomStyles subroutine shown in the following code draws a text string on a form. For each word in the string, RandomStyles uses randomly chosen color, size, bold, and italic attributes. |
|
|
|
 |
|
|
|
|
' Draw a string on the form using randomly chosen
' ForeColor, size, bold, and italic values. Start
' the text at Y position min_y and keep it
' between the margins min_x and max_x. Private Sub RandomStyles(txt As String, _
min_size As Integer, max_size As Integer, _
min_x As Single, max_x As Single, min_y As Single)
Dim length As Integer
Dim pos1 As Integer
Dim pos2 As Integer
Dim new_word As String
Dim clr As Long
Dim y As Integer
Dim font_names As Collection |
|
|
|
|
|